package com.woniuxy.yoga.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.Course;
import com.woniuxy.yoga.service.CourseService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author axq
 * @since 2020-10-23
 * @since 2020-10-27
 */
@CrossOrigin
@RestController
@RequestMapping("/yoga/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "俱乐部课程")
    @GetMapping(value = "/getCourse")
    public R getCourse(@ApiParam(value = "当前页") @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
                       @ApiParam(value = "每页的记录数") @RequestParam(value = "limit", required = true, defaultValue = "6") Integer limit,
                       Integer clubid) {
        //创建Page对象
        Page pageBean = new Page<>(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (clubid != null) {
            queryWrapper.like("clubid", clubid);
        }
        Page resultPage = courseService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if (records != null) {
            return R.ok().data("records", records).data("total", total);
        } else {
            return R.error().message("没有查到集合数据");
        }
    }

    @ApiOperation(value = "编辑课程")
    @GetMapping(value = "/UpdCoursebyid/{courseid}")
    public R updCoursebyid(@PathVariable("courseid") Integer courseid) {
        Course course = courseService.getById(courseid);
        if (course != null) {
            return R.ok().data("course", course);
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "确定修改课程")
    @PutMapping(value = "/UpdCourse")
    public R updCourse(@RequestBody Course course) {
        boolean age = courseService.updateById(course);
        if (age) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "添加课程")
    @PutMapping(value = "/addCourse/{clubid}")
    public R addCourse(@PathVariable("clubid")Integer clubid,Course course) {
        course.setClubid(clubid);
        boolean age = courseService.save(course);

        if (age) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }
}

